package org.crawler.processor;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Comparator;
import java.util.Map;
import java.util.Queue;
import java.util.TreeMap;

import org.crawler.Counter;
import org.crawler.task.TaskCenter;
import org.crawler.taskobj.DataRecord;
import org.crawler.taskobj.HtmlPage;
import org.crawler.taskobj.ImageFile;
import org.crawler.taskobj.WebUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MultiFileWriter extends Thread implements IProcess {

	@Autowired
	private TaskCenter taskCenter;
	
	private static long fileNameSeq=0;
	
	public void run()
	{
		while(true)
		{
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			innerProcess();
		}
	}
	
	@Override
	public void innerProcess() {
		
		DataRecord temp=null;
		while(taskCenter.getDataRecQueue().getTaskCount()>0)
		{
			temp=(DataRecord)taskCenter.getDataRecQueue().get();
			write2File(temp);
			fileNameSeq++;
		}
		
		while(taskCenter.getRecord().getTaskCount()>0)
		{
			String tempStr=(String) taskCenter.getRecord().get();
			writeLine("E://dog3//url.txt",tempStr);
		}
		
		procCarIdMapWrite(taskCenter.getCaridNameMap().getBandIdMap(),"E://car//band_id_name.txt");
		procCarIdMapWrite(taskCenter.getCaridNameMap().getSubBandIdMap(),"E://car//subband_id_name.txt");
		procCarIdMapWrite(taskCenter.getCaridNameMap().getSeriesIdMap(),"E://car//series_id_name.txt");
		procImageQueueWrite2(taskCenter.getImageQueue().getTaskQueue(),"E://car//logo//");

	}
	
	public void write2File(DataRecord dr)
	{
		File temp=new File("E:\\dog\\"+dr.getId()+"_"+dr.getName()+"_"+dr.getKey()+".txt");
		FileOutputStream outs=null;
		try {
			 outs=new FileOutputStream(temp);
			try {
				outs.write(dr.getVal().getBytes());
				outs.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public void writeLine(String fileName,String strLine)
	{
		FileWriter temp=null;
		try {
			temp = new FileWriter(fileName,true);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		strLine=strLine+"\r\n";

		try {
			temp.write(strLine);
			temp.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void procCarIdMapWrite(Map<String, String> srcMap,String FileName)
	{
		Map<String,String> tMap=srcMap;
		
		FileWriter fw =null;
		try {
			fw =new FileWriter(FileName, true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		while(tMap.size()>0)
		{
			 for (Map.Entry<String, String> entry : tMap.entrySet()) {
				   //System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
				   String key=entry.getKey();
				   String val=tMap.remove(key);
				   try {
					fw.write(key+"     "+val+"\r\n");
					Counter.nWriteStr++;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				   
			}
		}
		try {
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	public void procImageQueueWrite(Queue<ImageFile> queue,String dirName) 
	{
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		byte[] buffer = null;
		while(queue.size()>0)
		{
			ImageFile image=queue.poll();
			output.reset();
			buffer = new byte[4096];
			
			if(image==null)
			{
				System.out.println("Find Null ImageFile In ImageQueue");
				continue;
			}
			System.out.println("image_File_name:"+image.getFileName());
			
			
			
		    int n = 0;
		    try {
				while (-1 != (n = image.getIpts().read(buffer))) {
				    output.write(buffer, 0, n);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		    
		    String fileName=image.getBandId()+"_"+image.getBandName()+".jpg";
		    File imageFile = new File(dirName+fileName);  
		    FileOutputStream outStream;
			try {
				outStream = new FileOutputStream(imageFile);
				
			    outStream.write(output.toByteArray());
			    outStream.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  	      

		    
			
		}
	}
	
	public void procImageQueueWrite2(Queue<ImageFile> queue,String dirName) 
	{
		while(queue.size()>0)
		{
			ImageFile image=queue.poll();
		    String fileName=image.getBandId()+"_"+image.getBandName()+".jpg";
		    File imageFile = new File(dirName+fileName);  
		    FileOutputStream outStream = null;
			try {
				outStream = new FileOutputStream(imageFile);
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		    try {
				outStream.write(image.getImageBytes());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    try {
				outStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


}

